package com.yun.baymax.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.yun.baymax.R;
import com.yun.baymax.bean.adapter.LocationInfo;
import com.yun.baymax.utils.ToastUtils;
import com.yun.lib.utils.L;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * AMapV2地图中简单介绍一些Marker的用法.
 */
public class MarkerActivity extends AppCompatActivity implements AMap.OnMapLoadedListener {
    private MarkerOptions markerOption;
    private AMap aMap;
    private MapView mapView;
    private LatLng latlng;
    private String username;
    private LocationInfo locationInfo;

    public static void start(Context context, String username) {
        Intent i = new Intent(context, MarkerActivity.class);
        i.putExtra("username", username);
        context.startActivity(i);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        username = getIntent().getStringExtra("username");
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState); // 此方法必须重写
        init();
        fetchData();
    }

    private void fetchData() {
        if (TextUtils.isEmpty(username))
            return;
        BmobQuery<LocationInfo> query = new BmobQuery<>();
        query.addWhereEqualTo("userId", username);
        query.findObjects(new FindListener<LocationInfo>() {
            @Override
            public void done(List<LocationInfo> list, BmobException e) {
                if (e == null) {
                    if (list == null)
                        return;
                    locationInfo = list.get(0);
                    latlng = new LatLng(locationInfo.getLatitude(), locationInfo.getLongitude());
                    addMarkersToMap();// 往地图上添加marker
                } else {
                    L.i(e.getMessage());
                    ToastUtils.showShortToast("获取位置信息失败");
                }
            }
        });
    }

    /**
     * 初始化AMap对象
     */
    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
            aMap.setOnMapLoadedListener(this);
        }
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 在地图上添加marker
     */
    private void addMarkersToMap() {
        // 设置所有maker显示在当前可视区域地图中
        CameraUpdate cameraUpdate = CameraUpdateFactory
                .newCameraPosition(new CameraPosition(latlng, 18, 0, 0));
        aMap.moveCamera(cameraUpdate);

        //markerOption.icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
        //.decodeResource(getResources(),R.drawable.location_marker)));
        // 将Marker设置为贴地显示，可以双指下拉地图查看效果
        //markerOption.setFlat(true);//设置marker平贴地图效果

        markerOption = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(BitmapFactory
                .decodeResource(getResources(), R.drawable.marker)))
                .position(latlng)
                .title(locationInfo.getAddress())
                .snippet(locationInfo.getUpdatedAt())
                .draggable(true);
        aMap.addMarker(markerOption);
    }

    @Override
    public void onMapLoaded() {
    }
}
